
#include "IEC101ComuStatus.h"
#include "IEC101ProtocalDefineV1.h"
#include "crc.h"

#include "assert.h"
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <math.h>


uint16_t m_u16LinkAddress = 0x0007;
uint16_t m_nRECWaveAddr = 0x6001;

enumIEC101ComStatus m_Status_1;
enumIEC101DetailStatus m_Status_2;

bool m_bMasFCB;
bool m_bSlaFCB;

FIXEDFRAME m_sCMDTerminal_ReqLinkStatus;				 // ��վ_������·״̬;
FIXEDFRAME m_sCMDTerminal_ReqResetLink;					 // ��վ_����λ��·;
FIXEDFRAME m_sRESTerminal_ResLinkStatus;				 // ��վ_��Ӧ��·״̬;
FIXEDFRAME m_sRESTerminal_ResResetLink;					 // ��վ_��Ӧ��λ��·;
FIXEDFRAME m_sRESTerminal_ResCallonAct;					 // ��վ_��Ӧ���ٻ�����;
CMD_InitializedOver_Type m_sCMDTerminal_InitializedOver; // ��վ_��ʼ������;
FIXEDFRAME m_sRESTerminal_ResHeartBeat;					 // ��վ_��Ӧ������;
FIXEDFRAME m_sRESTerminal_ResTimeSync;					 // ��վ_��Ӧʱ��ͬ��;
FIXEDFRAME m_sRESTerminal_ResBurstSOE;					 // ��վ_��Ӧͻ��SOE;
FIXEDFRAME m_sRESTerminal_ResBurstRemoteMeasure;		 // ��վ_��Ӧͻ��ң��;

CMD_RealRecWave m_sRE_T2S_RECWave_ACT; // Ӧ��¼������

SENDED_COMMAND m_SendedCMD;

//--------------------��������-------------------
FIXEDFRAME m_sRESTerminal_ResponseWithOk;
FILEACTON_T updataFileActInfo;
UPDATEDATA_T updateData;
// uint8_t *updateFileContent = NULL;
UPDATEFILECONTENT_T m_pSoftwareUpdate;
UPDATEDATA_T m_pSoftwareUpdateData;

//--------------------��������---------------------
void MakePackForUpdateWriteFileActonConfirm(DataPack_Type *pSendFrame, uint16_t u16LinkAddress, FILEACTON_T *pFileActInfo, RESULTDES_T result)
{
	/*ȷ���ײ���Ϣ������*/
	FRAME_VAR_HEAD *pFrameVar = (FRAME_VAR_HEAD *)pSendFrame->ucData;
	uint16_t byteOfDataSizeInPack = 11 + pFileActInfo->Info.fileNameLen; // �ṹ���г��ļ�����������ṹ���Ա�Ĵ�С
	pFrameVar->ucStartChar1 = 0x68;
	pFrameVar->ucLen1 = (unsigned char)(sizeof(FRAME_VAR_HEAD) + byteOfDataSizeInPack - 4);
	pFrameVar->ucLen2 = (unsigned char)(sizeof(FRAME_VAR_HEAD) + byteOfDataSizeInPack - 4);
	pFrameVar->ucStartChar2 = 0x68;
	m_bSlaFCB = !m_bSlaFCB;
	unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData); // ���������
	pFrameVar->ucLinkControl = ucLinkControl;
	pFrameVar->u16LinkAddress = u16LinkAddress;	   // ��·��ַ
	pFrameVar->ucTI = eTI_F_FR_NA_1_FileTransport; // ��������
	pFrameVar->ucVSQ = 0x00;
	;
	pFrameVar->u16COT = eCOT_actcon; // ����ԭ��
	pFrameVar->u16ASDUAddr = u16LinkAddress;
	unsigned char *pucBuf = pSendFrame->ucData + sizeof(FRAME_VAR_HEAD);
	*((uint8_t *)pucBuf) = 0x08; // д�ļ�����ȷ��
	pucBuf++;
	*((uint8_t *)pucBuf) = (uint8_t)result;
	pucBuf++;
	*((uint8_t *)pucBuf) = pFileActInfo->Info.fileNameLen;
	pucBuf++;
	for (int i = 0; i < pFileActInfo->Info.fileNameLen; i++)
	{

		*((uint8_t *)pucBuf) = pFileActInfo->Info.fileName[i];
		pucBuf += sizeof(uint8_t);
	}
	// �ļ�ID
	*((uint8_t *)pucBuf) = (pFileActInfo->Info.fileID) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pFileActInfo->Info.fileID >> 8) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pFileActInfo->Info.fileID >> 16) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pFileActInfo->Info.fileID >> 24) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pFileActInfo->Info.fileLen) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pFileActInfo->Info.fileLen >> 8) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pFileActInfo->Info.fileLen >> 16) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pFileActInfo->Info.fileLen >> 24) & (0xFF);
	pucBuf++;
	// ����У����
	*pucBuf = CacuCheckCS(&(pFrameVar->ucLinkControl), pFrameVar->ucLen1);
	pucBuf++;
	*pucBuf = 0x16; // ��������־
	int nLength = pucBuf - pSendFrame->ucData + 1;
	pSendFrame->nSize = nLength; // ���¼��֡���ܳ���
}

void MakePackForUpdateImageDataConfirm(DataPack_Type *pSendFrame, uint16_t u16LinkAddress, UPDATEDATA_T *pImageDataInfo, IMAGESTATUS_T result)
{
	/*ȷ���ײ���Ϣ������*/
	FRAME_VAR_HEAD *pFrameVar = (FRAME_VAR_HEAD *)pSendFrame->ucData;
	uint8_t byteOfDataSizeInPack = 10; // д�ļ����ݴ���ȷ�ϵ��ֽ���
	pFrameVar->ucStartChar1 = 0x68;
	pFrameVar->ucLen1 = (unsigned char)(sizeof(FRAME_VAR_HEAD) + byteOfDataSizeInPack - 4);
	pFrameVar->ucLen2 = (unsigned char)(sizeof(FRAME_VAR_HEAD) + byteOfDataSizeInPack - 4);
	pFrameVar->ucStartChar2 = 0x68;
	m_bSlaFCB = !m_bSlaFCB;
	unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData); // ���������
	pFrameVar->ucLinkControl = ucLinkControl;
	pFrameVar->u16LinkAddress = u16LinkAddress;	   // ��·��ַ
	pFrameVar->ucTI = eTI_F_FR_NA_1_FileTransport; // ��������
	pFrameVar->ucVSQ = 0x00;
	pFrameVar->u16COT = eCOT_req; // ����ԭ��
	pFrameVar->u16ASDUAddr = u16LinkAddress;
	unsigned char *pucBuf = pSendFrame->ucData + sizeof(FRAME_VAR_HEAD);
	*((uint8_t *)pucBuf) = 0x0A; // д�ļ����ݴ���ȷ��

	pucBuf++;
	// �ļ�ID
	*((uint8_t *)pucBuf) = (pImageDataInfo->fileID) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pImageDataInfo->fileID >> 8) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pImageDataInfo->fileID >> 16) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pImageDataInfo->fileID >> 24) & (0xFF);
	// �κ�
	pucBuf++;
	*((uint8_t *)pucBuf) = (pImageDataInfo->fileSecNo) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pImageDataInfo->fileSecNo >> 8) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pImageDataInfo->fileSecNo >> 16) & (0xFF);
	pucBuf++;
	*((uint8_t *)pucBuf) = (pImageDataInfo->fileSecNo >> 24) & (0xFF);
	// ���������
	pucBuf++;
	*((uint8_t *)pucBuf) = (uint8_t)result;
	// ����У����
	pucBuf++;
	*pucBuf = CacuCheckCS(&(pFrameVar->ucLinkControl), pFrameVar->ucLen1);
	pucBuf++;
	*pucBuf = 0x16; // ��������־
	int nLength = pucBuf - pSendFrame->ucData + 1;
	pSendFrame->nSize = nLength; // ���¼��֡���ܳ���
}

void SetWaitConfirmCMD(SENDED_COMMAND *sendedCom, void *pucCMDBuf, unsigned char __ucCMDLength, enumIEC101WaitType __eWait)
{
	if (__ucCMDLength > IEC101CMD_MAXLENGTH)
		return;

	if (NULL == pucCMDBuf)
		memset(sendedCom->ucCMDBuf, 0, IEC101CMD_MAXLENGTH);
	else
		memcpy(sendedCom->ucCMDBuf, pucCMDBuf, __ucCMDLength);

	sendedCom->ucCMDLength = __ucCMDLength;
	sendedCom->eWait = __eWait;
	// ���ӻ�ȡϵͳʱ��Ĵ���sendedCom->waitStartTime  = CTime::GetCurrentTime();
	// RTC_getDate(&(sendedCom->waitStartTime.year),&(sendedCom->waitStartTime.month),&(sendedCom->waitStartTime.day));
	// RTC_getTime(&(sendedCom->waitStartTime.hour),&(sendedCom->waitStartTime.min),&(sendedCom->waitStartTime.sec));

	sendedCom->nReTryTimes = 0;
}

void MakeDefaultCommand(uint16_t __u16LinkAddress)
{
	//------------------------------------------------------------------------------------------------------
	unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, FALSE, FALSE, eFC_Request_LinkState);
	MakeFixFrame(&m_sCMDTerminal_ReqLinkStatus, ucLinkControl, __u16LinkAddress);
	ucLinkControl = MakeLinkControl(TRUE, TRUE, FALSE, FALSE, eFC_ResetRemoteLink);
	MakeFixFrame(&m_sCMDTerminal_ReqResetLink, ucLinkControl, __u16LinkAddress);
	//------------------------------------------------------------------------------------------------------
	ucLinkControl = MakeLinkControl(TRUE, FALSE, FALSE, FALSE, eFC_Response_LinkState); // ��Ӧ_��·״̬);
	MakeFixFrame(&m_sRESTerminal_ResLinkStatus, ucLinkControl, __u16LinkAddress);
	ucLinkControl = MakeLinkControl(TRUE, FALSE, FALSE, FALSE, eFC_Confirm_OK); // eFC_ȷ��_�Ͽ�);
	MakeFixFrame(&m_sRESTerminal_ResResetLink, ucLinkControl, __u16LinkAddress);
	//------------------------------------------------------------------------------------------------------
	ucLinkControl = MakeLinkControl(TRUE, FALSE, FALSE, FALSE, eFC_Confirm_OK);	 // eFC_ȷ��_�Ͽ�);
	MakeFixFrame(&m_sRESTerminal_ResHeartBeat, ucLinkControl, __u16LinkAddress); // m_sRES��վ_��Ӧ������
	//------------------------------------------------------------------------------------------------------
	// ��ʼ������������վ��һ�η�������ȷ�ϡ����ģ�Ҳ���� m_bSlaFCB = TRUE;
	ucLinkControl = MakeLinkControl(TRUE, TRUE, TRUE, TRUE, eFC_Send_Confirm_UserData);								 // eFC_����_ȷ��_�û�����
	MakePackForInitializedOver(&m_sCMDTerminal_InitializedOver, ucLinkControl, __u16LinkAddress, eCOI_LocalPowerOn); // m_sCMD��վ_��ʼ������

	//------------------------------------------------------------------------------------------------------
	ucLinkControl = MakeLinkControl(TRUE, FALSE, FALSE, FALSE, eFC_Confirm_OK); // eFC_ȷ��_�Ͽ�);
	MakeFixFrame(&m_sRESTerminal_ResTimeSync, ucLinkControl, __u16LinkAddress); // m_sRES��վ_��Ӧʱ��ͬ��
	//------------------------------------------------------------------------------------------------------
	ucLinkControl = MakeLinkControl(TRUE, FALSE, FALSE, FALSE, eFC_Confirm_OK);	 // eFC_ȷ��_�Ͽ�);
	MakeFixFrame(&m_sRESTerminal_ResCallonAct, ucLinkControl, __u16LinkAddress); // m_sRES��վ_��Ӧ���ٻ�����

	//------------------------------------------------------------------------------------------------------
	ucLinkControl = MakeLinkControl(TRUE, FALSE, FALSE, FALSE, eFC_Confirm_OK); // eFC_ȷ��_�Ͽ�);
	MakeFixFrame(&m_sRESTerminal_ResBurstSOE, ucLinkControl, __u16LinkAddress); // m_sRES��վ_��Ӧͻ��SOE
	//------------------------------------------------------------------------------------------------------
	ucLinkControl = MakeLinkControl(TRUE, FALSE, FALSE, FALSE, eFC_Confirm_OK);			  // eFC_ȷ��_�Ͽ�);
	MakeFixFrame(&m_sRESTerminal_ResBurstRemoteMeasure, ucLinkControl, __u16LinkAddress); // m_sRES��վ_��Ӧͻ��ң��

	//-------------------------------------ʵʱ¼������----------------------------------------------------------------
	ucLinkControl = MakeLinkControl(TRUE, FALSE, FALSE, FALSE, eFC_Confirm_OK);
	/*Get current system time*/
	TimeStruct curTime;
	// RTC_getDate(&curTime.year,&curTime.month, &curTime.day);
	// RTC_getTime(&curTime.hour,&curTime.min,   &curTime.sec);
	MakePackForCMD_RealRecWave(&m_sRE_T2S_RECWave_ACT, ucLinkControl, __u16LinkAddress, &curTime, eCOT_actcon);

	//-------------------------------------��������----------------------------------
	ucLinkControl = MakeLinkControl(TRUE, FALSE, FALSE, FALSE, eFC_Confirm_OK);
	MakeFixFrame(&m_sRESTerminal_ResponseWithOk, ucLinkControl, __u16LinkAddress);
}
void ParseSoftwareUpdateImageData(uint8_t *pFrame, UPDATEDATA_T *pDataInfo)
{
	uint32_t first = 0, second = 0, third = 0, fourth = 0;
	uint8_t frameDataLen = 0;
	uint32_t start = 0, end = 0;
	uint8_t checkCode = 0;
	uint8_t index = 0;
	uint8_t *pStartHold = (uint8_t *)(pFrame + sizeof(FRAME_VAR_HEAD));
	uint8_t *pStart = pStartHold;
	uint16_t crcCheckCode = 0;
	pDataInfo->optID = *pStart;
	// ��ȡ�ļ�ID
	pStart++;
	first = *pStart;
	pStart++;
	second = *pStart;
	pStart++;
	third = *pStart;
	pStart++;
	fourth = *pStart;
	pDataInfo->fileID = (fourth << 24) | (third << 16) | (second << 8) | first;
	// ȡ���ݶκ�
	pStart++;
	first = *pStart;
	pStart++;
	second = *pStart;
	pStart++;
	third = *pStart;
	pStart++;
	fourth = *pStart;
	pDataInfo->fileSecNo = (fourth << 24) | (third << 16) | (second << 8) | first;
	// ��ȡ������־
	pStart++;
	pDataInfo->haveMoreData = *pStart;
	// ȡ����֡�е���������
	//	if(1 == pDataInfo->haveMoreData)
	//	{
	///	frameDataLen = *(pFrame+1)-sizeof(FRAME_VAR_HEAD)-11+4;
	//}
	//	else{
	//		frameDataLen = updataFileActInfo.Info.fileLen - pDataInfo->fileSecNo;
	//	}
	//
	start = m_pSoftwareUpdate.nextStoreIndex;
	end = start + frameDataLen;
	// ��ȡ1K�����ݣ���д��FLASH��

	for (; start < end; start++)
	{
		pStart++;
		// pDataInfo->Info.data[index] = *pStart;
		m_pSoftwareUpdate.updateFileContent[start] = *pStart; // ��ʱע��
		checkCode += *pStart;
	}
	// У����
	pStart++;
	if (checkCode == *pStart)
	{
#ifdef DEBUG_IEC
		printf("\r\n�յ�������ʼ��%d, ��СΪ��%d, У��Ϸ�\r\n", pDataInfo->fileSecNo, frameDataLen);
#endif
		m_pSoftwareUpdate.nextStoreIndex = end;
	}
	else
	{
#ifdef DEBUG_IEC
		printf("\r\n�յ�������ʼ��%d, ��СΪ��%d, У�鲻�Ϸ�\r\n", pDataInfo->fileSecNo, frameDataLen);
#endif
	}

	if (end >= updataFileActInfo.Info.fileLen)
	{
		// crcУ��

		crcCheckCode = u16CRC_Calc16(m_pSoftwareUpdate.updateFileContent, updataFileActInfo.Info.fileLen);
#ifdef DEBUG_IEC
		printf("\r\n CRCУ������: %x \r\n", crcCheckCode);
#endif
		m_pSoftwareUpdate.imageReady = TRUE;
#ifdef DEBUG_IEC
		printf("\r\nӳ���ļ�ȫ�����գ����潫������FLASH\r\n");
#endif
	}
}

void ParseSoftwareUpdateWriteFileActonInfo(uint8_t *pFrame, FILEACTON_T *pFileInfo)
{
	uint8_t fileNameLen = 0;
	uint8_t index = 0;
	uint32_t first = 0, second = 0, third = 0, fourth = 0;
	uint32_t fileLength = 0;
	uint8_t *pStart = (uint8_t *)(pFrame + sizeof(FRAME_VAR_HEAD));
	pFileInfo->Info.optID = *pStart;
	pStart++;
	fileNameLen = *pStart;
	pFileInfo->Info.fileNameLen = fileNameLen;
	for (index = 0; index < fileNameLen; index++)
	{
		pStart++;
		pFileInfo->Info.fileName[index] = *pStart;
	}
	pStart++;
	first = *pStart;
	pStart++;
	second = *pStart;
	pStart++;
	third = *pStart;
	pStart++;
	fourth = *pStart;
	// �����ļ�ID��
	pFileInfo->Info.fileID = (fourth << 24) | (third << 16) | (second << 8) | first;
	// ���ļ���С
	pStart++;
	first = *pStart;
	pStart++;
	second = *pStart;
	pStart++;
	third = *pStart;
	pStart++;
	fourth = *pStart;
	pFileInfo->Info.fileLen = (fourth << 24) | (third << 16) | (second << 8) | first;
}

void SendProtocolFrameToMainStation(Comm_Type comType, unsigned char *data, uint16_t size)
{

	uint16_t index;
	switch (comType)
	{
	case enum_WiredNetwork:
		//			for(index = 0; index < size; index++)
		//		      _4G_UART_printf("%x",data[index]);
		// UART_Send(UART_1,data,size,BLOCKING);

#ifdef DEBUG_IEC
		printf("\r\n���͸���������\r\n");

		for (index = 0; index < size; index++)
			printf("%x, ", data[index]);

#endif
		// send(sock,data,size, 0);
		break;
	case enum_4G:
		// UART_Send(UART_1,data,size,BLOCKING);
		//		  for(index = 0; index < size; index++)
		//		      _4G_UART_printf("%x",data[index]);

		break;
	default:
#ifdef DEBUG_IEC
		printf("\r\nδ֪��ͨ�ŷ�ʽ");
#endif
		break;
	}
}

int ParseStatusFromFrameVar(FRAME_VAR_HEAD *pFrameVar)
{

	bool eCOT_Err;

	assert(NULL != pFrameVar);
	if (NULL == pFrameVar)
		return NEED_PARSE_NOTHING;

	LinkControlContent LCC;
	ParseLinkControl(pFrameVar->ucLinkControl, &LCC);
#ifdef DEBUG_IEC
	printf("\r\n�ն���·��ַ�ǣ�%x\r\n", m_u16LinkAddress);
#endif
	COT_Detail CTD;
	ParseCOT(pFrameVar->u16COT, &CTD);
	eCOT_Err = FALSE;
	switch (CTD.eCOT)
	{
	case eCOT_unknownTID:
#ifdef DEBUG_IEC
		printf("\r\n�豸����·��ַ=%x�����豸��COT�ֶ�(0x%02X)����:δ֪�����ͱ�ʶ", m_u16LinkAddress, pFrameVar->u16COT);
#endif
		eCOT_Err = TRUE;
		break;
	case eCOT_unknownCOT:
#ifdef DEBUG_IEC
		printf("�豸����·��ַ=%x�����豸��COT�ֶ�(0x%02X)����:δ֪�Ĵ���ԭ��", m_u16LinkAddress, pFrameVar->u16COT);
#endif
		eCOT_Err = TRUE;
		break;
	case eCOT_unknownASPUcommAddr:
#ifdef DEBUG_IEC
		printf("�豸����·��ַ=%x�����豸��COT�ֶ�(0x%02X)����:δ֪��Ӧ�÷������ݵ�Ԫ������ַ", m_u16LinkAddress, pFrameVar->u16COT);
#endif
		eCOT_Err = TRUE;
		break;
	case eCOT_unknownInfoObjAddr:
#ifdef DEBUG_IEC
		printf("�豸����·��ַ=%x�����豸��COT�ֶ�(0x%02X)����:δ֪����Ϣ�����ַ", m_u16LinkAddress, pFrameVar->u16COT);
#endif
		eCOT_Err = TRUE;
		break;
	case eCOT_RomoteControlStatusErr:
#ifdef DEBUG_IEC
		printf("�豸����·��ַ=%x�����豸��COT�ֶ�(0x%02X)����:δ֪����Ϣ�����ַ", m_u16LinkAddress, pFrameVar->u16COT);
#endif
		eCOT_Err = TRUE;
		break;
	case eCOT_RemoteControlTimeStampErr:
#ifdef DEBUG_IEC
		printf("�豸����·��ַ=%x�����豸��COT�ֶ�(0x%02X)����:δ֪����Ϣ�����ַ", m_u16LinkAddress, pFrameVar->u16COT);
#endif
		eCOT_Err = TRUE;
		break;
	case eCOT_RemoteControlDigitalSignAuthenErr:
#ifdef DEBUG_IEC
		printf("�豸����·��ַ=%x�����豸��COT�ֶ�(0x%02X)����:δ֪����Ϣ�����ַ", m_u16LinkAddress, pFrameVar->u16COT);
#endif
		eCOT_Err = TRUE;
		break;
	case eCOT_UnDefined:
#ifdef DEBUG_IEC
		printf("�豸����·��ַ=%x�����豸��COT�ֶ�(0x%02X)����:δ֪����Ϣ�����ַ", m_u16LinkAddress, pFrameVar->u16COT);
#endif
		eCOT_Err = TRUE;
		break;
	default:
		break;
	}
	if (TRUE == eCOT_Err)
	{
		return NEED_PARSE_NOTHING;
	}
	if (LCC.bFCV)
	{

		if (m_bMasFCB != LCC.bFCB)
			m_bMasFCB = LCC.bFCB;
		else
		{
#ifdef DEBUG_IEC
			printf("�豸����·��ַ=%x�����豸�յ���վ�ط�֡,���ͱ�ʶ(0x%02X),����ԭ��(0x%02X),��Ӱ��������ݴ���,..", m_u16LinkAddress, pFrameVar->ucTI, pFrameVar->u16COT);
#endif
			// return NEED_PARSE_NOTHING;
		}
	}

	/*1011
	����������
				  (�յ�)�û�����-��������(����)  	 => 68 0D 0D 68 73 02 00 D3 01 06 00 02 00 00 00 80 D1 16
				 �����ͣ�ȷ���Ͽ�	                     <= 10 80 02 00 82 16
				 �����ͣ��û�����-��������ȷ������(����ȷ��)	 <= 68 0D 0D 68 F3 02 00 D3 01 07 00 02 00 00 00 80 52 16
				  (�յ�)��վȷ���Ͽ�	                     => 10 00 02 00 02 16
  ��ֹ������
		   ���յ����û�����-��ֹ����(����)<= 68 0D 0D 68 73 02 00 D3 01 08 00 02 00 00 00 00 53 16
				 �����ͣ�ȷ���Ͽ�	                     => 10 80 02 00 82 16
				 �����ͣ��û�����-��ֹ����(ȷ��)<= 68 0D 0D 68 F3 02 00 D3 01 09 00 02 00 00 00 00 D4 16
				  (�յ�)��վȷ���Ͽ�	                     => 10 00 02 00 02 16

	д�ļ����䣺
	step 1:д�ļ�����
		   ���
			  FaultDetector.bin (����17��45 61 75 6C 74 44 65 74 65 63 74 6F 72 2E 62 69 6E )
			  ������ʶ��07
						�ļ������ȣ�11
						�ļ�����45 61 75 6C 74 44 65 74 65 63 74 6F 72 2E 62 69 6E ��FaultDetector.bin ASCII�룩
			  �ļ�ID��01 00 �����ֽ���ǰ��0001��ʾ�����ļ� 0002��ʾ����¼���ļ���
			  �ļ���С��05 10 �����ֽ���ǰ��
					ȷ�ϣ�
						FaultDetector.bin (����17��45 61 75 6C 74 44 65 74 65 63 74 6F 72 2E 62 69 6E )
			  ������ʶ��08
						��������֣�0 ���ɹ���
						�ļ������ȣ�11
						�ļ�����45 61 75 6C 74 44 65 74 65 63 74 6F 72 2E 62 69 6E ��FaultDetector.bin ASCII�룩
			  �ļ�ID��01 00 �����ֽ���ǰ��0001��ʾ�����ļ� 0002��ʾ����¼���ļ���
			  �ļ���С��05 10 �����ֽ���ǰ��
					  (�յ����û�����-д�ļ������  => 68 23 23 68 73 02 00 D2 01 06 00 02 00 00 00 07 11 45 61 75 6C 74 44 65 74 65 63 74 6F 72 2E 62 69 6E 01 00 05 10 FD 16
			(����) ȷ���Ͽɣ�              <= 10 80 02 00 82 16
						(����) �û�����-д�ļ�����ȷ��  <= 68 24 24 68 F3 02 00 D2 01 07 00 02 00 00 00 08 00 11 45 61 75 6C 74 44 65 74 65 63 74 6F 72 2E 62 69 6E 01 00 05 10 9C 16
			(�յ�) ȷ���Ͽɣ�              =>  10 00 02 00 02 16
	step2:д�ļ����ݴ���
		   д�ļ����ݣ�
			  ������ʶ��09
						�ļ�ID��01 00
						���ݶκţ�C8 00 (�ļ�����ƫ��ָ��ֵ)
						������־�� 01
						�ļ����ݣ� 00 00 00 00 ���������� 00
						�����룺45
				д�ļ�����ȷ�ϣ�
					  ������ʶ��0A
						�ļ�ID��01 00
						���ݶκţ�C8 00
						��������֣�00

						���յ����û�����-д�ļ����� =>=>    68 23 23 68 73 02 00 D2 01 05 00 02 00 00 00 09 01 00 C8 00 01 00 44 65 74 65 63 74 6F 72 2E 62 69 6E 01 00 05 45 xx 16
						 (���ͣ�ȷ���Ͽɣ�          <= 10 80 02 00 82 16
						�����ͣ��û�����-д�ļ�����ȷ�ϡ�<= 68 XX XX 68 F3 02 00 D2 01 05 00 02 00 00 00 0A 01 00 C8 00 00 XX 16
						 (�յ�) ȷ���Ͽɣ�=>  10 00 02 00 02 16
  ����������
			(�յ�)�û�����-��������(����)  	 => 68 0D 0D 68 73 02 00 D3 01 06 00 02 00 00 00 00 51 16
				 �����ͣ�ȷ���Ͽ�	                     <= 10 80 02 00 82 16
				 �����ͣ��û�����-��������ȷ������(����ȷ��)	 <= 68 0D 0D 68 F3 02 00 D3 01 07 00 02 00 00 00 00 D2 16
				  (�յ�)��վȷ���Ͽ�	                     => 10 00 02 00 02 16
				  */
	// ��վ���ٻ�����

	switch (LCC.ucFC)
	{
	case eFC_Send_Confirm_UserData:
	{

		// ��վ��������
		if (eTI_F_SR_NA_1_SoftwareUpdate == pFrameVar->ucTI) // 211
		{
			// ����������
			if (eCOT_act == CTD.eCOT) // ����
			{
				// �ұ����е�CTYPE
				unsigned char ctype = *((unsigned char *)pFrameVar + (sizeof(FRAME_VAR_HEAD) + 2));
				if (ctype == 0x80)
				{
					m_Status_1 = eStatus_SoftwareUpdate;
					m_Status_2 = eSoftwareUpdate_Mas_Request_SoftwareUpdate;
					// �ն˶���վ���������ȷ�Ϲ̶�֡
					SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResponseWithOk, IEC101_FRAME_FIX_SIZE);
					m_Status_2 = eSoftwareUpdate_Mas_Request_Sla_Response;
					////rt_thread_delay(RT_TICK_PER_SECOND/100);//���100ms
					// �ն˸�����Ӧ֡
					CMD_SoftwareUpdateConfirm sUpdateConfirmStart;
					m_bSlaFCB = !m_bSlaFCB;
					unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData); // eFC_����_ȷ��_�û�����
					MakePackForCMD_SoftwareUpdateStartOrStopConfirm(&sUpdateConfirmStart, ucLinkControl, m_u16LinkAddress, eCOT_actcon, TRUE);
					SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&sUpdateConfirmStart, sizeof(CMD_SoftwareUpdateConfirm));
					m_Status_2 = eSoftwareUpdate_Sla_ConfirmAct;
					// ������һ��ָ��
					SetWaitConfirmCMD(&m_SendedCMD, &sUpdateConfirmStart, sizeof(CMD_SoftwareUpdateConfirm), eWaitConfirm);
#ifdef DEBUG_IEC
					printf("\r\n��վ�յ���վ���͵�\"����������������\",���лظ�ȷ�Ϻ�����100ms��ظ�\"��������ȷ�ϼ���\"����...");
#endif
				}
				else if (ctype == 0x00)
				{
					m_Status_1 = eStatus_Complete_SoftwareUpdate;
					m_Status_2 = eSoftwareUpdate_Mas_Request_Complete_SoftwareUpdate;
					// �ն˶���վ���������ȷ�Ϲ̶�֡
					SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResponseWithOk, IEC101_FRAME_FIX_SIZE);
					m_Status_2 = eSoftwareUpdate_Mas_Request_Complete_Sla_Response;
					// rt_thread_delay(RT_TICK_PER_SECOND/10);//���100ms
					// �ն˸�����Ӧ֡
					CMD_SoftwareUpdateConfirm sUpdateConfirmStart;
					m_bSlaFCB = !m_bSlaFCB;
					unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData); // eFC_����_ȷ��_�û�����
					MakePackForCMD_SoftwareUpdateStartOrStopConfirm(&sUpdateConfirmStart, ucLinkControl, m_u16LinkAddress, eCOT_actcon, FALSE);
					SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&sUpdateConfirmStart, sizeof(CMD_SoftwareUpdateConfirm));
					m_Status_2 = eSoftwareUpdate_Sla_Complete_ConfirmAct;
					// ������һ��ָ��
					SetWaitConfirmCMD(&m_SendedCMD, &sUpdateConfirmStart, sizeof(CMD_SoftwareUpdateConfirm), eWaitConfirm);
#ifdef DEBUG_IEC
					printf("\r\n��վ�յ���վ���͵�\"����������������\",���лظ�ȷ�Ϻ�����100ms��ظ�\"������������ȷ��\"����...");
#endif
				}
			}
			else if (eCOT_deact == CTD.eCOT)
			{

				m_Status_1 = eStatus_Terminate_SoftwareUpdate;
				m_Status_2 = eSoftwareUpdate_Mas_Request_TerminateSoftwareUpdate;
				// �ն˶���վֹͣ�������������ȷ��
				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResponseWithOk, IEC101_FRAME_FIX_SIZE);
				m_Status_2 = eSoftwareUpdate_Mas_Request_Terminate_Sla_Response;
				/**********************************
				 �����ｫ���յ��������ļ�ȫ��ɾ�����ָ�������ǰ��״̬����Ҫ�����ⲿ�ֹ���
				***********************************/
				////rt_thread_delay(RT_TICK_PER_SECOND/100);//���100ms
				// �ն˸�����Ӧ֡
				CMD_SoftwareUpdateConfirm sUpdateConfirmStart;
				m_bSlaFCB = !m_bSlaFCB;
				unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData); // eFC_����_ȷ��_�û�����
				MakePackForCMD_SoftwareUpdateStartOrStopConfirm(&sUpdateConfirmStart, ucLinkControl, m_u16LinkAddress, eCOT_deactcon, FALSE);

				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&sUpdateConfirmStart, sizeof(CMD_SoftwareUpdateConfirm));
				m_Status_2 = eSoftwareUpdate_Sla_Terminate_ConfirmAct;
				// ������һ��ָ��
				SetWaitConfirmCMD(&m_SendedCMD, &sUpdateConfirmStart, sizeof(CMD_SoftwareUpdateConfirm), eWaitConfirm);
#ifdef DEBUG_IEC
				printf("\r\n��վ�յ���վ���͵�\"ֹͣ������������\",���лظ�ȷ�Ϻ�����100ms��ظ�\"�ն�ֹͣ��������ȷ��\"����...");
#endif
			}
		}
		else if (eTI_F_FR_NA_1_FileTransport == pFrameVar->ucTI)
		{
			if (eStatus_SoftwareUpdate == m_Status_1)
			{
				// �����������ļ�
				if (eCOT_act == CTD.eCOT) // ����
				{
					// ȡ��������ʶ
					unsigned char optId = *((unsigned char *)(pFrameVar) + sizeof(FRAME_VAR_HEAD));
					if (optId == 7)
					{
						m_Status_2 = eSoftwareUpdate_Mas_WriteFile_Acton;
						// �ն˶���վ���������ȷ�Ϲ̶�֡
						SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResponseWithOk, IEC101_FRAME_FIX_SIZE);
						m_Status_2 = eSoftwareUpdate_Mas_WriteFile_Acton_Sla_Response;
						////rt_thread_delay(RT_TICK_PER_SECOND/100);//���100ms
						// ȡ������е�����
						ParseSoftwareUpdateWriteFileActonInfo((uint8_t *)pFrameVar, &updataFileActInfo);
						// �ն˸�����Ӧ֡
						// ����ռ䱣��
						// m_pSoftwareUpdate.updateFileContent = (uint8_t *)malloc(updataFileActInfo.Info.fileLen + 10);	//��ʱע��
						// m_pSoftwareUpdate.updateFileContent = update_buf;
						m_pSoftwareUpdate.updateContentSize = updataFileActInfo.Info.fileLen;
						m_pSoftwareUpdate.nextStoreIndex = 0;
						m_pSoftwareUpdate.nextProgramFlashIndex = 0;
						m_pSoftwareUpdate.imageReady = FALSE;
						/*************׼����������ƹ�Ҳ���*********************/
						// updatePingPangPrepare();
						/**********************************/
						DataPack_Type m_pSendFrame;
						MakePackForUpdateWriteFileActonConfirm(&m_pSendFrame, m_u16LinkAddress, &updataFileActInfo, enum_Succeed);

						SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&(m_pSendFrame.ucData), m_pSendFrame.nSize);
						m_Status_2 = eSoftwareUpdate_Mas_WriteFile_Acton_Sla_ConfirmAct;
						// ������һ��ָ��
						SetWaitConfirmCMD(&m_SendedCMD, (unsigned char *)&(m_pSendFrame.ucData), m_pSendFrame.nSize, eWaitConfirm);
#ifdef DEBUG_IEC
						printf("\r\n��վ�յ���վ���͵�\"�������������������\",���лظ�ȷ�Ϻ�����100ms��ظ�\"�����������ȷ�ϼ���\"����...");
#endif
					}
				}
				else if (eCOT_req == CTD.eCOT)
				{
// ��վ����������������
// ȡ��������ʶ
#ifdef DEBUG_IEC
					printf("\r\n�յ���վ���������ݱ���\r\n");
#endif
					unsigned char optId = *((unsigned char *)(pFrameVar) + sizeof(FRAME_VAR_HEAD));

					if (optId == 9) // ��������������
					{
#ifdef DEBUG_IEC
						printf("\r\n������Ϊ9������վ����ȷ��֡������\r\n");
#endif
						m_Status_2 = eSoftwareUpdate_Mas_Trans_Data;
						// �ն˶���վ���������ȷ�Ϲ̶�֡
						SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResponseWithOk, IEC101_FRAME_FIX_SIZE);
						m_Status_2 = eSoftwareUpdate_Mas_Trans_Data_Sla_Response;
						// �����е�ӳ������
						ParseSoftwareUpdateImageData((uint8_t *)pFrameVar, &updateData);
						// �ն˸�����Ӧ֡
						DataPack_Type m_pSendFrame;
						// m_pSoftwareUpdate.imageReady = TRUE;
						MakePackForUpdateImageDataConfirm(&m_pSendFrame, m_u16LinkAddress, &m_pSoftwareUpdateData, enum_Image_Succeed);

						SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&(m_pSendFrame.ucData), m_pSendFrame.nSize);

						m_Status_2 = eSoftwareUpdate_Mas_Trans_Data_Sla_Confirm;
						// ������һ��ָ��
						SetWaitConfirmCMD(&m_SendedCMD, (unsigned char *)&(m_pSendFrame.ucData), m_pSendFrame.nSize, eWaitConfirm);
#ifdef DEBUG_IEC
						printf("\r\n��վ�յ���վ���͵�\"���������������\",���лظ�ȷ�Ϻ�����100ms��ظ�\"������ݱ���ȷ��\"����...");
#endif
					}
				}
			}
		}

		/*
		22:22:14	�����û�����-�ٻ�����(����)  	 => 68 0C 0C 68 73 02 00 64 01 06 00 01 00 00 00 14 F5 16
		22:22:14	ȷ���Ͽ�	                     <= 10 80 02 00 82 16
		22:22:14	�����û�����-�ٻ�����(����ȷ��)	 <= 68 0C 0C 68 D3 02 00 64 01 07 00 01 00 00 00 14 56 16
		22:22:14	ȷ���Ͽ�	                     => 10 00 02 00 02 16
		22:22:14	�����û�����-������Ϣ(��Ӧվ�ٻ�)<= 68 2A 2A 68 F3 02 00 01 9F 14 00 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 AB 16
		22:22:14	ȷ���Ͽ�	                     => 10 00 02 00 02 16
		22:22:14	�����û�����-�̸�����(��Ӧվ�ٻ�)<= 68 60 60 68 D3 02 00 0D 91 14 00 01 00 01 40 00 00 00 00 40 3D 0A 47 41 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 00 00 00 00 40 D8 16
		22:22:14	ȷ���Ͽ�	                     => 10 00 02 00 02 16
		22:22:14	�����û�����-�ٻ�����(������ֹ)	 <= 68 0C 0C 68 F3 02 00 64 01 0A 00 01 00 00 00 14 79 16
		22:22:14	ȷ���Ͽ�	                     => 10 00 02 00 02 16
		*/
		// ��վ���ٻ�����
		else if (eTI_C_IC_NA_1_MainStationCallOnCommand == pFrameVar->ucTI)
		{
			// ��վҪ���ٻ���������
			if (eCOT_act == CTD.eCOT) // ����
			{
				m_Status_1 = eStatus_CallData;
				m_Status_2 = eCallData_Mas_ReqestData;
				// �ն�����վ��������

				// m_pSimu->Send(&m_sRES��վ_��Ӧ���ٻ�����,IEC101_FRAME_FIX_SIZE);
				////rt_thread_delay(RT_TICK_PER_SECOND/100);//�¼Ӵ��100ms
				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResCallonAct, IEC101_FRAME_FIX_SIZE);
				m_Status_2 = eCallData_Mas_ReqestData_SlaResponse;
				// Sleep(100);
				// rt_thread_delay(RT_TICK_PER_SECOND/100);//���100ms
				// CMD_���ٻ� sCMD��Ӧ���ٻ�;
				CMD_GeneralInterrogation sCMDCallOn;
				m_bSlaFCB = !m_bSlaFCB;
				unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData); // eFC_����_ȷ��_�û�����
				MakePackForGeneralInterrogation(&sCMDCallOn, ucLinkControl, m_u16LinkAddress, eCOT_actcon);
				////rt_thread_delay(RT_TICK_PER_SECOND/100);//�¼Ӵ��100ms
				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&sCMDCallOn, sizeof(CMD_GeneralInterrogation));
				m_Status_2 = eCallData_Sla_ConfirmAct;
				// ������һ��ָ��
				SetWaitConfirmCMD(&m_SendedCMD, &sCMDCallOn, sizeof(CMD_GeneralInterrogation), eWaitConfirm);
#ifdef DEBUG_IEC
				printf("\r\n��վ�յ���վ���͵�\"���ٻ���������\",���лظ�ȷ�Ϻ�����100ms��ظ�\"���ٻ�ȷ�ϼ���\"����...");
#endif
			}
		}
		else if (eTI_C_CS_NA_1_ClockSynCommand == pFrameVar->ucTI) // ʱ��ͬ������
		{
			if (eCOT_act == CTD.eCOT) // ����
			{
				m_Status_1 = eStatus_TimeData;
				m_Status_2 = eTimeCollect_Mas_SendTime;
				assert(sizeof(CMD_TimeSyn) == pFrameVar->ucLen1 + 6);
				CMD_TimeSyn *pPack = (CMD_TimeSyn *)pFrameVar;
				TimeStruct tTime = ParseCP56Time2a(pPack->st);
#ifdef DEBUG_IEC
				printf("��վҪ���ʱ[%4d-%02d-%02d %02d:%02d:%02d]", tTime.year, tTime.month, tTime.day, tTime.hour, tTime.min, tTime.sec);
#endif
				// ����ʵʱʱ�ӵ�ʱ��
				// RTC_setDate(tTime.year,tTime.month,tTime.day);
				// RTC_setTime(tTime.hour,tTime.min,tTime.sec);

				// ������վ��Ӧ����
				// delay_ms(RT_TICK_PER_SECOND/10);//�¼Ӵ��100ms
				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResTimeSync, IEC101_FRAME_FIX_SIZE);
				m_Status_2 = eTimeCollect_Mas_SendTime_SlaResponse;
				// ˯һ��ʱ��󣬾ͷ��ͼ���ȷ�ϱ���
				// Sleep(100);
				// rt_thread_delay(RT_TICK_PER_SECOND/10);//���100ms
				CMD_TimeSyn sCMDTimeSynActOn;
				// CMD_ʱ��ͬ�� sCMDʱ��ͬ��������;
				m_bSlaFCB = !m_bSlaFCB;
				unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData); // eFC_����_ȷ��_�û�����
				MakePackForTimeSyn(&sCMDTimeSynActOn, ucLinkControl, m_u16LinkAddress, eCOT_actcon, pPack->st);
				// sCMDʱ��ͬ��������.MakePack(ucLinkControl,m_u16LinkAddress,eCOT_����ȷ��_actcon,pPack->stʱ��);
				// m_pSimu->Send(&sCMDʱ��ͬ��������,sizeof(CMD_ʱ��ͬ��));
				// rt_thread_delay(RT_TICK_PER_SECOND/10);//�¼Ӵ��100ms
				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&sCMDTimeSynActOn, sizeof(CMD_TimeSyn));
				// ��ʱ�ɹ����ȴ���վȷ�ϱ���
				m_Status_2 = eTimeCollect_Sla_SendTime;
				// ������һ��ָ��
				SetWaitConfirmCMD(&m_SendedCMD, &sCMDTimeSynActOn, sizeof(CMD_TimeSyn), eWaitConfirm);
				// m_SendedCMD.SetWaitConfirmCMD(&sCMDʱ��ͬ��������,sizeof(CMD_ʱ��ͬ��),eWaitConfirm);
			}
		}

		/*********************************-----¼������---****************************************************************************/
		else if (eTI_F_RE_WA_1_CallRecordData == pFrameVar->ucTI)
		{
			if (eCOT_act == CTD.eCOT)
			{
				m_Status_1 = eStatus_CallRealTimeRecWave;
				m_Status_2 = eCallRealTimeRecWave_Mas_Request_file;
				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResCallonAct, IEC101_FRAME_FIX_SIZE);
				m_Status_2 = eCallRealTimeRecWave_Mas_Request_file_Sla_Response;
				// rt_thread_delay(RT_TICK_PER_SECOND/10);

				TimeStruct curTime;
				// RTC_getDate(&curTime.year,&curTime.month, &curTime.day);
				// RTC_getTime(&curTime.hour,&curTime.min,   &curTime.sec);

				m_bSlaFCB = !m_bSlaFCB;
				unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData);
				MakePackForCMD_RealRecWave(&m_sRE_T2S_RECWave_ACT, ucLinkControl, m_u16LinkAddress, &curTime, eCOT_actcon);
				//::CacuCheckCS((unsigned char*)&m_sRE_T2S_RECWave_ACT.FVH.u16LinkAddress, m_sRE_T2S_RECWave_ACT.FVH.ucLen1);
				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRE_T2S_RECWave_ACT, sizeof(CMD_RealRecWave));

				m_Status_2 = eCallRealTimeRecWave_Sla_Read_File_Activation_Confirm;
				// cache above command for resending when timeout
				SetWaitConfirmCMD(&m_SendedCMD, &m_sRE_T2S_RECWave_ACT, sizeof(CMD_RealRecWave), eWaitConfirm);
#ifdef DEBUG_IEC
				printf("Slave: Received the [CALL_RECORD_WAVE] from Master, 100ms later, Send [CALL_RW_DATA_ACT]");
#endif
			}
			else if (eCOT_actterm == CTD.eCOT)
			{
				m_Status_2 = eCallRealTimeRecWave_Mas_Call_Data_STOP;
				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResCallonAct, IEC101_FRAME_FIX_SIZE);
				m_Status_2 = eCallRealTimeRecWave_Mas_Call_Data_STOP_Confirm_Sla_Response;
				// Sleep(100);
#ifdef DEBUG_IEC
				printf("Slave: Received the [STOP_RECORD_WAVE_CMD] from Master, 100ms later, Send [COMFIRM]");
#endif
				g_pRecordWaveFrameList.nSendedIndex = -1;
				ClearStatusAndWaitForNothing();
			}
			/*������������ֹͣ¼���� ��ͬʱ���ն����������������ݣ��⼸������֡��Ҫ����������ȷ��*/
		}
		/*********************************------------------�ϴ���ʷ¼������Э�����---------------****************************************************************************/
		else if (eTI_F_RE_WA_2_CallRecordData == pFrameVar->ucTI)
		{
			if (eCOT_act == CTD.eCOT)
			{
				m_Status_1 = eStatus_CallPastRecWave;
				m_Status_2 = eCallPastRecWave_Mas_Request_file;
				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResCallonAct, IEC101_FRAME_FIX_SIZE);

				m_Status_2 = eCallPastRecWave_Mas_Request_file_Sla_Response;
				// rt_thread_delay(RT_TICK_PER_SECOND/10);

				/*Get current system time*/
				TimeStruct curTime;
				// RTC_getDate(&curTime.year,&curTime.month, &curTime.day);
				// RTC_getTime(&curTime.hour,&curTime.min,   &curTime.sec);

				m_bSlaFCB = !m_bSlaFCB;
				unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData);

				MakePackForCMD_RealRecWave(&m_sRE_T2S_RECWave_ACT, ucLinkControl, m_u16LinkAddress, &curTime, eCOT_actcon);
				//::CacuCheckCS((unsigned char*)&m_sRE_T2S_RECWave_ACT.FVH.u16LinkAddress, m_sRE_T2S_RECWave_ACT.FVH.ucLen1);
				SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRE_T2S_RECWave_ACT, sizeof(CMD_RealRecWave));

				m_Status_2 = eCallPastRecWave_Sla_Read_File_Activation_Confirm;
				// ������һ������
				SetWaitConfirmCMD(&m_SendedCMD, &m_sRE_T2S_RECWave_ACT, sizeof(CMD_RealRecWave), eWaitConfirm);
			}
			/*  else if (eCOT_????_actterm == CTD.eCOT)
			  {
				  m_Status_2 = eCallPastRecWave_Mas_Call_Data_STOP;
				  m_pSimu->Send(&m_sRES??_???????, IEC101_FRAME_FIX_SIZE);
				  m_Status_2 = eCallPastRecWave_Mas_Call_Data_STOP_Confirm_Sla_Response;
				  //Sleep(100);
				  m_pSimu->OutputMessage("?????????\"?????????\",??????...");
				  ClearStatusAndWaitForNothing();
				  //??????????????,???????????,???????????????
			  }*/
		}
	}
	break;
	}
	return NEED_PARSE_NOTHING;
}

void ClearStatusAndWaitForNothing()
{
	m_Status_1 = eStatus_WaitForData;
	m_Status_2 = eDetailNothing;
	// ������һ��ָ��
	SetWaitConfirmCMD(&m_SendedCMD, NULL, 0, eWaitNothing);
	// m_SendedCMD.SetWaitConfirmCMD(NULL,0,eWaitNothing);
}

void ParseStatusFromFrameFix(FIXEDFRAME *pFrameFix)
{
	LinkControlContent LCC;

	ParseLinkControl(pFrameFix->ucLinkControl, &LCC);
	switch (LCC.ucFC)
	{
		// case eFC_��λԶ����·:
		//  case eFC_ȷ��_�Ͽ�:
	case eFC_Confirm_OK:
	{
		// PRMΪ1,������Ϊ0,��ʾ������վ���͵ġ���λԶ����·������
		if (LCC.bPRM)
		{
#ifdef DEBUG_IEC
			printf("\r\n�յ�:��վ������·��λ;����:��վ��Ӧ��·��λ,����100ms��,��վ����վ������·״̬...");
#endif
			// ��վ����ľ���Ҫ����·��λ ��վ-����վ ��·��λ
			// ����״̬
			m_Status_1 = eStatus_ShakeHand;
			m_Status_2 = eShakeHand_Mas_ResetLink;
			// ��վ��Ӧ��·��λ           ��վ-����վ ��Ӧ��·��λ

			// m_pSimu->Send(&m_sRES��վ_��Ӧ��λ��·,IEC101_FRAME_FIX_SIZE);
			/*LCC=80*/
			SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResResetLink, IEC101_FRAME_FIX_SIZE);
			m_Status_2 = eShakeHand_Mas_ResetLink_SlaResponse;
			// rt_thread_delay(RT_TICK_PER_SECOND/10);//���100ms
			// ��վ��������·״̬

			SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sCMDTerminal_ReqLinkStatus, IEC101_FRAME_FIX_SIZE);
			m_Status_2 = eShakeHand_Sla_ReqestLinkStatus;
			// �ȴ���վ��Ӧ�ն˵���·����
			// ������һ��ָ��
			SetWaitConfirmCMD(&m_SendedCMD, &m_sCMDTerminal_ReqResetLink, IEC101_FRAME_FIX_SIZE, eWaitResponse);
			// m_SendedCMD.SetWaitConfirmCMD(&m_sCMD��վ_������·״̬,IEC101_FRAME_FIX_SIZE,eWaitResponse);
		}
		else
		{
			// PRMΪ0,����վ����վ����Ӧ
			switch (m_Status_1)
			{
			case eStatus_SoftwareUpdate:
			{
				if (eSoftwareUpdate_Sla_ConfirmAct == m_Status_2)
				{
#ifdef DEBUG_IEC
					printf("\r\n��վ����������������ȷ�ϣ���վ������Ӧ����������վ������������\"\"");
#endif
					m_Status_2 = eSoftwareUpdate_Sla_ConfirmAct_Mas_Response;
					SetWaitConfirmCMD(&m_SendedCMD, NULL, 0, eWaitResponse);
				}
				else if (eSoftwareUpdate_Mas_WriteFile_Acton_Sla_ConfirmAct == m_Status_2)
				{
#ifdef DEBUG_IEC
					printf("\r\n��վ������������д�ļ�����ȷ�ϣ���վ������Ӧ����վ�ȴ���վ������\"\"");
#endif
					m_Status_2 = eSoftwareUpdate_Sla_WriteFile_ConfirmAct_Mas_Response;
					SetWaitConfirmCMD(&m_SendedCMD, NULL, 0, eWaitResponse);
				}
				else if (eSoftwareUpdate_Mas_Trans_Data_Sla_Confirm == m_Status_2)
				{
#ifdef DEBUG_IEC
					printf("\r\n��վ����д�ļ����ݴ���ȷ�ϣ���վ������Ӧ����վ�ȴ���վ����һ֡����\"\"");
#endif
					m_Status_2 = eSoftwareUpdate_Mas_Trans_Data_Sla_Confirm_mas_response;
					SetWaitConfirmCMD(&m_SendedCMD, NULL, 0, eWaitResponse);
				}
			}
			break;
			case eStatus_Terminate_SoftwareUpdate:
			{
				if (eSoftwareUpdate_Sla_Terminate_ConfirmAct == m_Status_2)
				{
#ifdef DEBUG_IEC
					printf("\r\n��վ����ֹͣ����������ȷ�ϣ���վ������Ӧ��");
#endif
					m_Status_2 = eSoftwareUpdate_Sla_Terminate_ConfirmAct_Mas_Response;
				}
			}
			break;
			case eStatus_Complete_SoftwareUpdate:
			{
				if (eSoftwareUpdate_Sla_Complete_ConfirmAct == m_Status_2)
				{
#ifdef DEBUG_IEC
					printf("\r\n��վ�����������������ȷ�ϣ���վ������Ӧ��");
#endif
					m_Status_2 = eSoftwareUpdate_Sla_Complete_ConfirmAct_Mas_Response;
					ClearStatusAndWaitForNothing();
				}
			}
			break;

				// ���ֹ���
			case eStatus_ShakeHand:
			{
				// ��վ��Ӧ��·��λ
				if (eShakeHand_Sla_ResetLink == m_Status_2)
				{
#ifdef DEBUG_IEC
					printf("\r\n��վ������·��λ,��վ������Ӧ, ��������վ����\"��ʼ����������\"");
#endif
					m_Status_2 = eShakeHand_Sla_ResetLink_MasResponse;
					// ��վ���ͳ�ʼ����������
					// rt_thread_delay(RT_TICK_PER_SECOND/100);//�¼Ӵ��100ms
					SendProtocolFrameToMainStation(enum_WiredNetwork, m_sCMDTerminal_InitializedOver.data, sizeof(CMD_InitializedOver_Type));
					m_Status_2 = eShakeHand_Sla_ReportInitializeOver;
					// �ȴ� ��վȷ����վ��ʼ������
					m_bSlaFCB = TRUE; // TRUE;
					SetWaitConfirmCMD(&m_SendedCMD, &m_sCMDTerminal_InitializedOver, sizeof(CMD_InitializedOver_Type), eWaitConfirm);
					// m_SendedCMD.SetWaitConfirmCMD(&m_sCMD��վ_��ʼ������,sizeof(CMD_��ʼ������),eWaitConfirm);
				}
				else if (eShakeHand_Sla_ReportInitializeOver == m_Status_2)
				{
#ifdef DEBUG_IEC
					printf("��վȷ����վ��ʼ�����������ֳɹ�...");
#endif
					ClearStatusAndWaitForNothing();
				}
			}
			break;
			case eStatus_SponSOE:
			{
				m_Status_2 = eSponSignal_Sla_SendSponSOE_MasResponse;
#ifdef DEBUG_IEC
				printf("��վȷ�Ͻ���ͻ��SOE...");
#endif
				ClearStatusAndWaitForNothing();
			}
			break;
			case eStatus_SponMeasure:
			{
				m_Status_2 = eSponMeasure_Sla_SendSponMeasure_MasResponse;
#ifdef DEBUG_IEC
				printf("��վȷ�Ͻ���ͻ��ң����...");
#endif
				ClearStatusAndWaitForNothing();
			}
			break;
			// ���ٻ�����
			case eStatus_CallData:
			{
				if (eCallData_Sla_ConfirmAct == m_Status_2)
				{
					m_Status_2 = eCallData_Sla_ConfrimAct_MasResponse;
#ifdef DEBUG_IEC
					printf("\r\n��վȷ����վ���ٱ�����,��վ������(100ms)�������ϴ�ң�š�ң������...");
#endif
					m_Status_2 = eCallData_Sla_RemoteSignal;
					// Sleep(100);
					////rt_thread_delay(RT_TICK_PER_SECOND/10);//���100ms
				}
				if (eCallData_Sla_RemoteSignal == m_Status_2)
				{
					m_Status_2 = eCallData_Sla_RemoteSignal_MasResponse;
					// ���ȷ���ң������
					int nSignalCallDataCount = g_pRemoteSignalSendAll.nTotalCount;
					bool bSignalSendOver = (0 == g_pRemoteSignalSendAll.nTotalCount) || (g_pRemoteSignalSendAll.nSendedIndex > nSignalCallDataCount - 1);
					if (FALSE)
					{
						if (g_pRemoteSignalSendAll.nSendedIndex < 0)
							g_pRemoteSignalSendAll.nSendedIndex = 0;

// ASSERT(m_pSimu->m_SST.CCSignalCallData.nSendedIndex <= nSignalCallDataCount-1);
#ifdef DEBUG_IEC
						printf("\r\n��վ����:ң������ [��%d��,�ܹ�%d����]...", g_pRemoteSignalSendAll.nSendedIndex + 1, nSignalCallDataCount);
#endif
						DataPack_Type *DP = g_pRemoteSignalSendAll.data;
						// ����FCB����״̬�����¼���У���
						m_bSlaFCB = !m_bSlaFCB;
						unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData);
						FRAME_VAR_HEAD *pFrameVar = (FRAME_VAR_HEAD *)DP->ucData;
						pFrameVar->ucLinkControl = ucLinkControl;
						DP->ucData[DP->nSize - 2] = CacuCheckCS(&pFrameVar->ucLinkControl, pFrameVar->ucLen1);
						// ��������
						// ��վ����ң������
						////rt_thread_delay(RT_TICK_PER_SECOND/10);//�¼Ӵ��100ms
						SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)DP->ucData, DP->nSize);
						m_Status_2 = eCallData_Sla_RemoteSignal;
						// ֡����
						g_pRemoteSignalSendAll.nSendedIndex++;
						// �ȴ���վ�ظ�
						SetWaitConfirmCMD(&m_SendedCMD, DP->ucData, DP->nSize, eWaitConfirm);
					}
					else
					{
						m_Status_2 = eCallData_Sla_RemoteMeasure;
#ifdef DEBUG_IEC
						printf("\r\n��վ����ң���������,��վ������(100ms)�������ϴ�ң������...");
#endif
						////rt_thread_delay(RT_TICK_PER_SECOND/50);//���100ms
						// RemoteMeasureUpdate();//����ң�����ݵ����λ��
					}
				}
				if (eCallData_Sla_RemoteMeasure == m_Status_2)
				{
					m_Status_2 = eCallData_Sla_RemoteMeasure_MasResponse;
					int nMeasureCallDataCount = g_pRemoteMeasureAll.nTotalCount;
					bool bMeasureSendOver = (0 == g_pRemoteMeasureAll.nTotalCount) || (g_pRemoteMeasureAll.nSendedIndex > nMeasureCallDataCount - 1);
					if (!bMeasureSendOver)
					{
						if (g_pRemoteMeasureAll.nSendedIndex < 0)
							g_pRemoteMeasureAll.nSendedIndex = 0;

						assert((g_pRemoteMeasureAll.nSendedIndex <= nMeasureCallDataCount - 1));

// RemoteMeasureUpdate();
#ifdef DEBUG_IEC
						printf("\r\n��վ����:ң������ [��%d��,�ܹ�%d����]...", g_pRemoteMeasureAll.nSendedIndex + 1, nMeasureCallDataCount);
#endif
						DataPack_Type *DP = g_pRemoteMeasureAll.data;
						// ����FCB����״̬�����¼���У���
						m_bSlaFCB = !m_bSlaFCB;
						unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData);
						FRAME_VAR_HEAD *pFrameVar = (FRAME_VAR_HEAD *)DP->ucData;
						pFrameVar->ucLinkControl = ucLinkControl;
						DP->ucData[DP->nSize - 2] = CacuCheckCS(&pFrameVar->ucLinkControl, pFrameVar->ucLen1);
						// ��������

						// ��վ����ң������
						////rt_thread_delay(RT_TICK_PER_SECOND/10);//�¼Ӵ��100ms
						SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)DP->ucData, DP->nSize);
						m_Status_2 = eCallData_Sla_RemoteMeasure;
						// ֡����
						g_pRemoteMeasureAll.nSendedIndex++;
						// �ȴ���վ�ظ�
						SetWaitConfirmCMD(&m_SendedCMD, DP->ucData, DP->nSize, eWaitConfirm);
					}
					else
					{
#ifdef DEBUG_IEC
						printf("\r\nң�ź�ң�����ݶ��Ѿ��������,����(100ms)�󣬷��͡����ٻ�����������");
#endif
						// rt_thread_delay(RT_TICK_PER_SECOND/10);//���100ms
						CMD_GeneralInterrogation CMD_CallOver;
						m_bSlaFCB = !m_bSlaFCB;
						unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData);
						MakePackForGeneralInterrogation(&CMD_CallOver, ucLinkControl, m_u16LinkAddress, eCOT_actterm);
						// ��վ����
						////rt_thread_delay(RT_TICK_PER_SECOND/10);//�¼Ӵ��100ms
						SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&CMD_CallOver, sizeof(CMD_GeneralInterrogation));

						m_Status_2 = eCallData_Sla_End;
						SetWaitConfirmCMD(&m_SendedCMD, &CMD_CallOver, sizeof(CMD_GeneralInterrogation), eWaitConfirm);

						// g_pRemoteSignalSendAll.nSendedIndex = -1;
						g_pRemoteMeasureAll.nSendedIndex = -1;
					}
				}
				if (eCallData_Sla_End == m_Status_2)
				{
					m_Status_2 = eCallData_Sla_End_MasResponse;
#ifdef DEBUG_IEC
					printf("\r\n��վȷ��ң�ź�ң�����ݶ��Ѿ��������,���ٻ���������...");
#endif
					// ����ȴ�״̬
					ClearStatusAndWaitForNothing();
				}
			}
			break;
			case eStatus_TimeData:
			{
#ifdef DEBUG_IEC
				printf("\r\n�յ�:��վ��Ӧ��ʱ�������ʱ����...");
#endif
				assert(eTimeCollect_Sla_SendTime == m_Status_2);
				m_Status_2 = eTimeCollect_Mas_SendTime_SlaResponse;
				// ��ʱ�����������ȴ�
				ClearStatusAndWaitForNothing();
				// �����ϱ�ʶ����ΪTrue����¼�������ϴ�
				TimeStruct currTime;
				// RTC_getTime(&currTime.hour,&currTime.min, &currTime.sec);
				srand(currTime.hour * 24 * 3600 + currTime.min * 60 + currTime.sec);
#ifdef DEBUG_IEC
				int k = rand();
				printf("\r\n�������ֵΪ%d...", k);
				if (k % 5 == 3)
				// AD_FaultRecord(sampleCircleSequence.Writepoint,20);
#elif
				if (rand() % 5 == 3)
					// AD_FaultRecord(sampleCircleSequence.Writepoint,20);
					if ((reportFaultPoint.hasFault == 1) && (reportFaultPoint.faultCyclePos <= 3500) && (reportFaultPoint.faultPointInSecond < 100))
					{
						// AD_FaultRecord(reportFaultPoint.faultPointInSecond,reportFaultPoint.faultCyclePos);
					}
#endif

					break;

				//********************************************************2018.11.5*****************************************************************************************
			case eStatus_Fault_Report: // �����·���ϱ���
			{
				if (eReportFault_Sla_Reporting == m_Status_2) // ��վ���ն˵Ĺ��ϱ�����Ӧ
				{
					m_Status_2 = eReportFault_Mas_Response;
					SetWaitConfirmCMD(&m_SendedCMD, NULL, 0, eWaitResponse);
#ifdef DEBUG_IEC
					printf("\r\n Master confirms  reception of Fault_Report,  (After 100ms)upload Record Data...");
#endif
					// rt_thread_delay(RT_TICK_PER_SECOND/10);
					m_Status_2 = eReportFault_Sla_RecordWave;
				}
				if (eReportFault_Sla_End == m_Status_2)
				{
					m_Status_2 = eReportFault_Sla_End_MasResponse;
#ifdef DEBUG_IEC
					printf("\r\n Master confirms the END MSG, The precedure of Record END...");
#endif
					SetWaitConfirmCMD(&m_SendedCMD, NULL, 0, eWaitResponse);
					// m_RecordWaveEndTime = CIEC101BH_ZHHZ_V1_Simu::getTimeMillisec();// Only for simulation
					// buf_fault_block.isTanferComplete=TRUE;
					ClearStatusAndWaitForNothing();
				}

				if (eReportFault_Sla_RecordWave == m_Status_2)
				{
					SetWaitConfirmCMD(&m_SendedCMD, NULL, 0, eWaitResponse);
					m_Status_2 = eReportFault_Sla_ReportFault_MasResponse;

					/*�������¼�����ݻ�û�з�����ϣ���������*/
					/*if(buf_fault_block.indexOfCurrentPosition<=SAMPLE_COUNT_PER_FAULT_BLOCK)
					{
   #ifdef DEBUG_IEC
					   printf("\r\n Slave Sending: RecordWave Frame [NO:%d, Total Count: %d]",(uint16_t)ceil(((float)(buf_fault_block.indexOfCurrentPosition+1))/SAMPLE_COUNT_PER_RECORD_WAVE_DATA_FRAME),(uint16_t)ceil(((float)SAMPLE_COUNT_PER_FAULT_BLOCK)/SAMPLE_COUNT_PER_RECORD_WAVE_DATA_FRAME));
   #endif
						DataPack_Type* DP            = &g_RecordWaveFrame;
						bool  isLastFrame             = FALSE;
						uint8_t byteOfDataSizeInPack = SIZE_RECORD_WAVE_DATA_FRAME;

						if(SAMPLE_COUNT_PER_FAULT_BLOCK-buf_fault_block.indexOfCurrentPosition<=SAMPLE_COUNT_PER_RECORD_WAVE_DATA_FRAME)
						{
							isLastFrame          = TRUE;
							//byteOfDataSizeInPack = BYTES_PER_FAULT_BLOCK - (buf_fault_block.indexOfCurrentPosition)*24;
							byteOfDataSizeInPack = (SAMPLE_COUNT_PER_FAULT_BLOCK-buf_fault_block.indexOfCurrentPosition)*24;
						}
//										 else
//										 {
//											 //buf_fault_block.indexOfCurrentPosition += SAMPLE_COUNT_PER_RECORD_WAVE_DATA_FRAME;//ָ����һ��¼�����ݱ������������ݿ�Ŀ�ʼλ��
//										 }

						MakePackRecordDataForFault(DP,isLastFrame,m_u16LinkAddress,buf_fault_block.indexOfCurrentPosition,byteOfDataSizeInPack);
						   //����FCB����״̬�����¼���У���
						   m_bSlaFCB = !m_bSlaFCB;
						   unsigned char ucLinkControl = MakeLinkControl(TRUE,TRUE,m_bSlaFCB,TRUE,eFC_Send_Confirm_UserData);
						   RECORD_WAVE_HEAD* pFrameVar = (RECORD_WAVE_HEAD*)DP->ucData;
						   pFrameVar->FVH.ucLinkControl = ucLinkControl;
						   DP->ucData[DP->nSize - 2] = CacuCheckCS(&pFrameVar->FVH.ucLinkControl, pFrameVar->FVH.ucLen1);
						   //��վ����¼������֡
						   ////rt_thread_delay(RT_TICK_PER_SECOND/100);
						   SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char*)DP->ucData,DP->nSize);
						   m_Status_2 = eReportFault_Sla_RecordWave;

						   //�ȴ���վ�ظ�
						   SetWaitConfirmCMD(&m_SendedCMD,DP->ucData,DP->nSize,eWaitConfirm);

						if(SAMPLE_COUNT_PER_FAULT_BLOCK-buf_fault_block.indexOfCurrentPosition>SAMPLE_COUNT_PER_RECORD_WAVE_DATA_FRAME)
						{
							   buf_fault_block.indexOfCurrentPosition += SAMPLE_COUNT_PER_RECORD_WAVE_DATA_FRAME;//ָ����һ��¼�����ݱ������������ݿ�Ŀ�ʼλ��
						}else
						{
							 buf_fault_block.indexOfCurrentPosition = SAMPLE_COUNT_PER_FAULT_BLOCK+1;
						}


					}
					else //¼������֡�ѷ������
					{
						CMD_AbnormityReport CMD_RECWAVEOver;
						//rt_thread_delay(RT_TICK_PER_SECOND/10);
						#ifdef DEBUG_IEC
						printf("\r\n Slave END Send: 100 ms later, send CMD_RECWAVE_END frame");
						#endif
						m_bSlaFCB = !m_bSlaFCB;
						unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData);


						TimeStruct curTime;
						//RTC_getDate(&curTime.year,&curTime.month, &curTime.day);
						//RTC_getTime(&curTime.hour,&curTime.min,   &curTime.sec);
						MakePackForCMD_AbnormityReport(&CMD_RECWAVEOver, ucLinkControl, m_u16LinkAddress, &curTime,eCOT_actterm);
						SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char*)&CMD_RECWAVEOver,sizeof(CMD_AbnormityReport));

						m_Status_2 = eReportFault_Sla_End;
						SetWaitConfirmCMD(&m_SendedCMD,(unsigned char*)&CMD_RECWAVEOver, sizeof(CMD_AbnormityReport), eWaitConfirm);
						buf_fault_block.isTanferComplete=TRUE;

						buf_fault_block.indexOfCurrentPosition=0;
						reportFaultPoint.hasFault=0;
					}*/
				}
			} // END CASE
			break;
			case eStatus_CallRealTimeRecWave: // ʵʱ¼������
			{
				if (eCallRealTimeRecWave_Sla_Read_File_Activation_Confirm == m_Status_2)
				{
					m_Status_2 = eCallRealTimeRecWave_Sla_Read_File_Activation_Confirm_Mas_Response;
#ifdef DEBUG_IEC
					printf("\r\n Master has confirmed RECORD_WAVE ACTIVATION, 100 ms later, CMD_RECWAVE frame will be sent in sequence");
#endif
					m_Status_2 = eCallRealTimeRecWave_Sla_Trans_File;

					// ptrBlockStartPOs        = &(sampleCircleSequence.sampleQueue[sampleCircleSequence.Writepoint].electrical_data[0]);
					//ptrCurrPos = ptrBlockStartPOs;
					// currPOs.currentSecond   = sampleCircleSequence.Writepoint;

					// rt_thread_delay(RT_TICK_PER_SECOND/100);
				}

				if (eCallRealTimeRecWave_Sla_Trans_File == m_Status_2) // ����¼������֡
				{
					SetWaitConfirmCMD(&m_SendedCMD, NULL, 0, eWaitNothing);
					m_Status_2 = eCallRealTimeRecWave_Sla_Trans_File_Mas_Response;

					//								 if (g_pRecordWaveFrameList.nSendedIndex < 0)
					//									 g_pRecordWaveFrameList.nSendedIndex = 0;
					//								 else
					//									 g_pRecordWaveFrameList.nSendedIndex = g_pRecordWaveFrameList.nSendedIndex %g_pRecordWaveFrameList.nTotalCount;

					////rt_thread_delay(RT_TICK_PER_SECOND/10);
#ifdef DEBUG_IEC
					printf("\r\n Slave: send [NO: %d] RECORD_DATA_FRAME", g_pRecordWaveFrameList.nSendedIndex);
#endif
					DataPack_Type *DP = &g_RecordWaveFrame;

					// MakePackForRealtimeRecordData (DP,FALSE, m_u16LinkAddress, SIZE_RECORD_WAVE_DATA_FRAME);
					m_bSlaFCB = !m_bSlaFCB;
					unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData);
					RECORD_WAVE_HEAD *pFrameVar = (RECORD_WAVE_HEAD *)DP->ucData;
					pFrameVar->FVH.ucLinkControl = ucLinkControl;
					pFrameVar->FVH.u16COT = eCOT_RecordData;
					pFrameVar->FVH.ucTI = eTI_F_RE_WA_1_CallRecordData;
					DP->ucData[DP->nSize - 2] = CacuCheckCS(&pFrameVar->FVH.ucLinkControl, pFrameVar->FVH.ucLen1);
					////rt_thread_delay(RT_TICK_PER_SECOND/10);

					SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)DP->ucData, DP->nSize);
					;
					m_Status_2 = eCallRealTimeRecWave_Sla_Trans_File;

					g_pRecordWaveFrameList.nSendedIndex++;
					SetWaitConfirmCMD(&m_SendedCMD, (unsigned char *)&DP->ucData, DP->nSize, eWaitConfirm);
				}
			}
			break;

			case eStatus_CallPastRecWave: // ����ʷ¼������
			{

				if (eCallPastRecWave_Sla_Read_File_Activation_Confirm == m_Status_2)
				{
					m_Status_2 = eCallPastRecWave_Sla_Read_File_Activation_Confirm_Mas_Response;
#ifdef DEBUG_IEC
					printf("\r\n Master: Confirm  RECORD_WAVE_ACTIVATION");
#endif
					m_Status_2 = eCallPastRecWave_Sla_Trans_File;
					// rt_thread_delay(RT_TICK_PER_SECOND/100);
				}

				if (eCallPastRecWave_Sla_Trans_File == m_Status_2) // ����¼������
				{
					SetWaitConfirmCMD(&m_SendedCMD, NULL, 0, eWaitNothing);
					m_Status_2 = eCallPastRecWave_Sla_Trans_File_Mas_Response;

					/*��Flash����¼�����ݺ���ʷ���ݷ��ڵ���������������������ϱ������ݲ���IEC101ͨ��*/
					int nCount = g_pRecordWaveFrameList.nTotalCount;
					bool bRecordWaveSendOver = (0 == g_pRecordWaveFrameList.nTotalCount) || (g_pRecordWaveFrameList.nSendedIndex > nCount - 1);

					if (!bRecordWaveSendOver)
					{
						if (g_pRecordWaveFrameList.nSendedIndex < 0)
							g_pRecordWaveFrameList.nSendedIndex = 0;

////rt_thread_delay(RT_TICK_PER_SECOND/20);
#ifdef DEBUG_IEC
						printf("\r\n Slave Sending: RecordWave Frame [NO:%d, Total: %d]", g_pRecordWaveFrameList.nSendedIndex + 1, nCount);
#endif

						DataPack_Type *DP = g_pRecordWaveFrameList.data;
						// ����FCB����״̬�����¼���У���
						m_bSlaFCB = !m_bSlaFCB;
						unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData);
						RECORD_WAVE_HEAD *pFrameVar = (RECORD_WAVE_HEAD *)DP->ucData;
						pFrameVar->FVH.ucLinkControl = ucLinkControl;
						DP->ucData[DP->nSize - 2] = CacuCheckCS(&pFrameVar->FVH.ucLinkControl, pFrameVar->FVH.ucLen1);
						// ��վ����¼������֡
						////rt_thread_delay(RT_TICK_PER_SECOND/20);
						SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)DP->ucData, DP->nSize);
						m_Status_2 = eCallPastRecWave_Sla_Trans_File;

						// ֡����
						g_pRecordWaveFrameList.nSendedIndex++;
						// �ȴ���վ�ظ�
						SetWaitConfirmCMD(&m_SendedCMD, DP->ucData, DP->nSize, eWaitConfirm);
					}

					else // �ն����ݷ������
					{
						CMD_AbnormityReport CMD_RECWAVEOver;
// rt_thread_delay(RT_TICK_PER_SECOND/100);
#ifdef DEBUG_IEC
						printf("\r\n Slave END Send: 100 ms later, send CMD_RECWAVE_END frame");
#endif
						m_bSlaFCB = !m_bSlaFCB;
						unsigned char ucLinkControl = MakeLinkControl(TRUE, TRUE, m_bSlaFCB, TRUE, eFC_Send_Confirm_UserData);

						/*Get current system time*/
						TimeStruct curTime;
						// RTC_getDate(&curTime.year,&curTime.month, &curTime.day);
						// RTC_getTime(&curTime.hour,&curTime.min,   &curTime.sec);
						MakePackForCMD_AbnormityReport(&CMD_RECWAVEOver, ucLinkControl, m_u16LinkAddress, &curTime, eCOT_actterm);
						SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&CMD_RECWAVEOver, sizeof(CMD_AbnormityReport));

						m_Status_2 = eCallPastRecWave_Sla_Trans_Data_End;
						SetWaitConfirmCMD(&m_SendedCMD, (unsigned char *)&CMD_RECWAVEOver, sizeof(CMD_AbnormityReport), eWaitConfirm);
						g_pRecordWaveFrameList.nSendedIndex = -1;
					}
				}

				if (eCallPastRecWave_Sla_Trans_Data_End == m_Status_2)
				{
					m_Status_2 = eCallPastRecWave_Sla_Trans_Data_End_Mas_Confirm;
#ifdef DEBUG_IEC
					printf("\r\n Master--Slave: Confirm  CMD_RECWAVE_END");
#endif
					SetWaitConfirmCMD(&m_SendedCMD, NULL, 0, eWaitResponse);
					ClearStatusAndWaitForNothing();
				}
			}
			break;
			default:
				break;
			}
			}
		}
		break;
		//-------------------------------------------------------------------------------------------------------------------------------------------------------------
	case eFC_Confirm_NO:
	{
#ifdef DEBUG_IEC
		printf("�յ�IEC101��֡\"���Ͽ�֡\"������,������(0x%02X),��·��ַ(0x%02X)", pFrameFix->ucLinkControl, LCC.ucFC);
#endif
	}
	break;
	case eFC_Send_Confirm_LinkTestFun:
	{
#ifdef DEBUG_IEC
		printf("�յ�:��վ������;����:��վ��������Ӧ");
#endif

		// ��վ->��վ ��Ӧ������
		// ��վ����
		////rt_thread_delay(2*RT_TICK_PER_SECOND/10);//���100ms
		SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResHeartBeat, IEC101_FRAME_FIX_SIZE);
		// RTC_getDate(&(timeSpanHeartBeat.year),&(timeSpanHeartBeat.month),&(timeSpanHeartBeat.day));
		// RTC_getTime(&(timeSpanHeartBeat.hour),&(timeSpanHeartBeat.min),&(timeSpanHeartBeat.sec));
		////rt_thread_delay(RT_TICK_PER_SECOND/10);//���100ms
		// ���״̬�󲻵ȴ��κγ�ʱ
		ClearStatusAndWaitForNothing();
	}
	break;
	case eFC_Request_LinkState:
	{
#ifdef DEBUG_IEC
		printf("\r\n��һ����������·״̬");
#endif
		if (pFrameFix->u16LinkAddress != m_u16LinkAddress)
		{
			if (0xFFFF != pFrameFix->u16LinkAddress)
			{
#ifdef DEBUG_IEC
				printf("\r\n<<��·��ַ��ƥ��!>>,���ն���·��ַ:0x%02X,����վ������·��ַ:0x%02X", m_u16LinkAddress, pFrameFix->u16LinkAddress);
#endif
				// �������ڵȴ�״̬
				ClearStatusAndWaitForNothing();
				break;
			}
			else
			{
#ifdef DEBUG_IEC
				printf("\r\n�յ�:��վ������·״̬(�㲥��ַ0xFFFF);����:��վ��Ӧ��·״̬");
#endif
			}
		}
		else
		{
#ifdef DEBUG_IEC
			printf("��·��ַƥ��,�յ�:��վ������·״̬;����:��վ��Ӧ��·״̬");
#endif
		}
		m_Status_1 = eStatus_ShakeHand;
		// ��վ����վ������·״̬ ��վ-����վ
		m_Status_2 = eShakeHand_Mas_ReqestLinkStatus;
		// ��������վ������·״̬��Ӧ ��վ-����վ
		// ��վ����
		// rt_thread_delay(RT_TICK_PER_SECOND/10);//�¼Ӵ��100ms
		SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sRESTerminal_ResLinkStatus, IEC101_FRAME_FIX_SIZE);
		// SendProtocolFrameToCommMailBox(recFrame->comm_type,(unsigned char*)&m_sRESTerminal_ResLinkStatus,IEC101_FRAME_FIX_SIZE);

		m_Status_2 = eShakeHand_Mas_ReqestLinkStatus_SlaResponse;
		// ������һ��ָ��
		SetWaitConfirmCMD(&m_SendedCMD, &m_sRESTerminal_ResLinkStatus, IEC101_FRAME_FIX_SIZE, eWaitResponse);
	}
	break;
	case eFC_Response_LinkState:
	{
#ifdef DEBUG_IEC
		printf("\r\n�յ�: ��վ��Ӧ��·״̬;����:��վ����λ��·");
#endif
		m_Status_1 = eStatus_ShakeHand;
		// ��վ�յ���վ����·״̬����Ӧ ��վ-����վ
		m_Status_2 = eShakeHand_Sla_ReqestLinkStatus_MasResponse;
		// ��������վ������·��λ���� ��վ-����վ
		// ��վ����
		// rt_thread_delay(RT_TICK_PER_SECOND/10);//�¼Ӵ��100ms
		SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&m_sCMDTerminal_ReqResetLink, IEC101_FRAME_FIX_SIZE);
		// SendProtocolFrameToMainStation(enum_WiredNetwork,(unsigned char*)&m_sCMDTerminal_ReqResetLink,IEC101_FRAME_FIX_SIZE);

		m_Status_2 = eShakeHand_Sla_ResetLink;
		// ������һ��ָ��
		SetWaitConfirmCMD(&m_SendedCMD, &m_sCMDTerminal_ReqResetLink, IEC101_FRAME_FIX_SIZE, eWaitConfirm);
	}
	break;
	}
	}
}

/*modified@shan: �ط���������IEC101_MAX_RETRY_TIMES����վ��û����Ӧ������255���򷵻��ط�����*/
uint32_t waitTotalSecs = 0;
uint8_t CheckTimeOutStatus(void)
{
	TimeStruct currTime;
	// RTC_getDate(&currTime.year,&currTime.month, &currTime.day);
	// RTC_getTime(&currTime.hour,&currTime.min, &currTime.sec);

	int ret = 0;
	waitTotalSecs = GetDifferenceTime(&currTime, &m_SendedCMD.waitStartTime);

	if (eWaitConfirm == m_SendedCMD.eWait || eWaitResponse == m_SendedCMD.eWait)
	{
		// totalSecs = GetDifferenceTime(&currTime, &m_SendedCMD.waitStartTime);
		if (waitTotalSecs > IEC101_RETRY_TIMEOUT)
		{
			if (m_SendedCMD.nReTryTimes >= IEC101_MAX_RETRY_TIMES)
			{
#ifdef DEBUG_IEC
				if (m_SendedCMD.ucCMDLength > 0)
					printf("��������%d�κ�,��վ�޷�Ӧ,��վֹͣ��Ӧ", m_SendedCMD.nReTryTimes);
				else
					printf("�ȴ�����%d��,��վ�޷�Ӧ,��վֹͣ��Ӧ", IEC101_RETRY_TIMEOUT * m_SendedCMD.nReTryTimes);
				// ��ɸ����ӵĴ��룺��վ����������·״̬����λ��·����
#endif
				m_Status_1 = eStatus_WaitForData;
				m_Status_2 = eDetailNothing;
				memset(&m_SendedCMD, 0, sizeof(SENDED_COMMAND));
				// ��վ����������·״̬����Ҫ�Ѽ�¼��վFCB״̬������վFCB״̬��BOOLֵ����Ϊ0
				m_bMasFCB = FALSE;
				m_bSlaFCB = FALSE;
				// ret=255;
			}
			else
			{

				if (m_SendedCMD.ucCMDLength > 0)
				{
					// ��վ����
					// rt_thread_delay(RT_TICK_PER_SECOND/10);//���100ms
					SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)m_SendedCMD.ucCMDBuf, m_SendedCMD.ucCMDLength);
					// m_pSimu->Send(m_SendedCMD.ucCMDBuf,m_SendedCMD.ucCMDLength);
#ifdef DEBUG_IEC
					printf("�ն˵ȴ���վ��Ӧ����%d��,��%d�����·�����һ������...", IEC101_RETRY_TIMEOUT, m_SendedCMD.nReTryTimes + 1);
#endif
				}
				else
				{
#ifdef DEBUG_IEC
					printf("�ն˵ȴ���վ��Ӧ������%d��%d��,", m_SendedCMD.nReTryTimes + 1, IEC101_RETRY_TIMEOUT);
#endif
				}

				m_SendedCMD.nReTryTimes++;
				// RTC_getTime(&m_SendedCMD.waitStartTime.hour,&m_SendedCMD.waitStartTime.min,&m_SendedCMD.waitStartTime.sec);
				ret = m_SendedCMD.nReTryTimes;
			}
		}
	}
	/*��������ѷ���֡��������һ���յ�����֡����600��*/
	if (waitTotalSecs > 240)
	{
		ret = 255;
		waitTotalSecs = 0;
	}
	return ret;
}

// rt_bool_t SendSOE()
//{
//	if(m_Status_1!=eStatus_WaitForData)
//		return RT_FALSE;

//	int nSignalSOEDataCount = (int)m_pSimu->m_SST.CCSignalSOEData.DataPackArray.size();
//	bool bSignalSendOver = m_pSimu->m_SST.CCSignalSOEData.DataPackArray.empty()||(m_pSimu->m_SST.CCSignalSOEData.nSendedIndex>nSignalSOEDataCount-1);
//	if(!bSignalSendOver)
//	{
//		if(m_pSimu->m_SST.CCSignalSOEData.nSendedIndex<0)
//			m_pSimu->m_SST.CCSignalSOEData.nSendedIndex = 0;

//		ASSERT(m_pSimu->m_SST.CCSignalSOEData.nSendedIndex <= nSignalSOEDataCount-1);

//		CString strInfo;
//		strInfo.Format("��վ����:ͻ��ң������ [��%d��,�ܹ�%d����]...",m_pSimu->m_SST.CCSignalSOEData.nSendedIndex+1,nSignalSOEDataCount);
//		m_pSimu->OutputMessage(strInfo);

//		DataPack& DP = m_pSimu->m_SST.CCSignalSOEData.DataPackArray[m_pSimu->m_SST.CCSignalSOEData.nSendedIndex];
//		//����FCB����״̬�����¼���У���
//		m_bSlaFCB = !m_bSlaFCB;
//		unsigned char ucLinkControl = CIEC101Parser::MakeLinkControl(true,true,m_bSlaFCB,true,eFC_����_ȷ��_�û�����);
//		FRAME_VAR_HEAD* pFrameVar = (FRAME_VAR_HEAD*)DP.ucData;
//		pFrameVar->ucLinkControl = ucLinkControl;
//		DP.ucData[DP.nSize-2] = ::CacuCheckCS(&pFrameVar->ucLinkControl,pFrameVar->ucLen1);
//		//��������
//		m_pSimu->Send(&DP.ucData,DP.nSize);
//		m_Status_1 = eStatus_SponSOE;
//		m_Status_2 = eSponSignal_Sla_SendSponSOE;
//		//֡����
//		m_pSimu->m_SST.CCSignalSOEData.nSendedIndex++;
//		//�ȴ���վ�ظ�
//		m_SendedCMD.SetWaitConfirmCMD(&DP.ucData,DP.nSize,eWaitConfirm);
//	}
//	else
//	{
//		m_pSimu->m_SST.CCSignalSOEData.nSendedIndex = -1;
//		m_pSimu->OutputMessage("����ͻ��SOE�Ѿ��������,�����¿�ʼ...");
//	}
//	return TRUE;
//}

// rt_bool_t SendMeasure()
//{
//	if(m_Status_1!=eStatus_WaitForData)
//		return RT_FALSE;

//	int nMeasureSOEDataCount = (int)m_pSimu->m_SST.CCMeasureSOEData.DataPackArray.size();
//	bool bMeasureSendOver = m_pSimu->m_SST.CCMeasureSOEData.DataPackArray.empty()||(m_pSimu->m_SST.CCMeasureSOEData.nSendedIndex>nMeasureSOEDataCount-1);
//	if(!bMeasureSendOver)
//	{
//		if(m_pSimu->m_SST.CCMeasureSOEData.nSendedIndex<0)
//			m_pSimu->m_SST.CCMeasureSOEData.nSendedIndex = 0;

//		ASSERT((m_pSimu->m_SST.CCMeasureSOEData.nSendedIndex <= nMeasureSOEDataCount-1));

//		CString strInfo;
//		strInfo.Format("��վ����:ң������ [��%d��,�ܹ�%d����]...",m_pSimu->m_SST.CCMeasureSOEData.nSendedIndex+1,nMeasureSOEDataCount);
//		m_pSimu->OutputMessage(strInfo);

//		DataPack& DP = m_pSimu->m_SST.CCMeasureSOEData.DataPackArray[m_pSimu->m_SST.CCMeasureSOEData.nSendedIndex];
//		//����FCB����״̬�����¼���У���
//		m_bSlaFCB = !m_bSlaFCB;
//		unsigned char ucLinkControl = CIEC101Parser::MakeLinkControl(true,true,m_bSlaFCB,true,eFC_����_ȷ��_�û�����);
//		FRAME_VAR_HEAD* pFrameVar = (FRAME_VAR_HEAD*)DP.ucData;
//		pFrameVar->ucLinkControl = ucLinkControl;
//		DP.ucData[DP.nSize-2] = ::CacuCheckCS(&pFrameVar->ucLinkControl,pFrameVar->ucLen1);
//		//��������
//		m_pSimu->Send(&DP.ucData,DP.nSize);
//		m_Status_1 = eStatus_SponMeasure;
//		m_Status_2 = eSponSignal_Sla_SendSponSOE_MasResponse;
//		//֡����
//		m_pSimu->m_SST.CCMeasureSOEData.nSendedIndex++;
//		//�ȴ���վ�ظ�
//		m_SendedCMD.SetWaitConfirmCMD(&DP.ucData,DP.nSize,eWaitConfirm);
//	}
//	else
//	{
//		m_pSimu->m_SST.CCMeasureSOEData.nSendedIndex = -1;
//		m_pSimu->OutputMessage("����ͻ��ң���Ѿ��������,�����¿�ʼ...");
//	}
//	return TRUE;
//}

bool SendFaultReportFrame(CP56Time2a *tm) // send a Record packet
{
	CMD_AbnormityReport cmdAbnormityReport;
	if (m_Status_1 != eStatus_WaitForData || m_Status_2 != eDetailNothing)
		return FALSE;

/*sending command*/
#ifdef DEBUG_IEC
	printf("\r\n Sending Fault Report frame\r\n");
#endif
	m_bSlaFCB = !m_bSlaFCB;
	unsigned char ucLinkControl = MakeLinkControl(1, 1, m_bSlaFCB, 1, eFC_Send_Confirm_UserData);
	MakePackForCMD_FaultReport(&cmdAbnormityReport, ucLinkControl, m_u16LinkAddress, tm, eCOT_spont);
	SendProtocolFrameToMainStation(enum_WiredNetwork, (unsigned char *)&cmdAbnormityReport, sizeof(CMD_AbnormityReport));

	/*set status of protocol state machine*/
	m_Status_1 = eStatus_Fault_Report;
	m_Status_2 = eReportFault_Sla_Reporting;

	/*cache the command into buffer for resending when timeout*/
	SetWaitConfirmCMD(&m_SendedCMD, &cmdAbnormityReport, sizeof(CMD_AbnormityReport), eWaitConfirm);
	return TRUE;
}

void Sim7600_IEC101_Init(void)
{
	// �����豸��ʼ��
	// rtc_init();
	// sim7600CE��ʼ��
	// sim7600ce_Init();
	MakeDefaultCommand(m_u16LinkAddress);
}
static uint8_t timesOfRestart4G = 0;
void IEC101_DataReceive(void)
{
	uint8_t *strx = NULL;
	uint16_t index = 0, bytes_received = 0;

	/*if(_4G_RX_STA&0X8000)		//���յ�һ��������
	{
		 _4G_UART_RX_BUF[_4G_RX_STA&0X7FFF]=0;//���ӽ�����
		   bytes_received = _4G_RX_STA&0X7FFF;
		   for(index = 0; index < bytes_received; index ++)
		 {
					   //rt_kprintf("%x, ",recv_data[index]);
			   InQueue(&IEC101RecSequeue,_4G_UART_RX_BUF[index]);
		 }
	 //receivedDataToIEC101Frame();
		   //strx = sim7600ce_check_cmd("CLOSED");//�������Ͽ�������
	 sim_at_response(1);
	 timesOfRestart4G=0;
	 receivedDataToIEC101Frame();
	 } */

	/*�Ƿ�ɸ�Ϊ������ѭ���������̣߳�����ģ����״̬������4Gģ��*/
	if (CheckTimeOutStatus() == 255 || strx != NULL)
	{
		// sim7600ce_Init();
		ClearStatusAndWaitForNothing();
		timesOfRestart4G++; // ͳ����ϵ���������������涨ֵ�������߳�
	}
}
